﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Arim.S4.Client.Base.Dto
{
    public class DtoHelper
    {
        public static Dictionary<string, string> GetDescriptions(Type dtoType)
        {
            PropertyInfo[] fields = dtoType.GetProperties();
            List<DisplayProperty> dps = new List<DisplayProperty>(2);
            foreach (PropertyInfo fi in fields)
            {
                object[] eds = fi.GetCustomAttributes(typeof(DisplayAttribute), true);
                if (eds.Length == 0)
                    continue;
                DisplayAttribute da = eds[0] as DisplayAttribute;
                dps.Add(new DisplayProperty() { Order = da.GetOrder() ?? 0, Name = da.Name, PropertyName = fi.Name });
            }
            return dps.OrderByDescending(s => s.Order).ToDictionary(s => s.PropertyName, t => t.Name);
        }

        private class DisplayProperty
        {
            public int Order { get; set; }
            public string Name { get; set; }
            public string PropertyName { get; set; }
        }
    }
}
